package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AssessHistory;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/5/12.
 */
public class BrowseAdapter extends BaseAdapter {

    private List<AssessHistory> mAssessHistories;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public BrowseAdapter(List<AssessHistory> assessHistories, Context context) {
        mAssessHistories = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<AssessHistory> mAssessHistories) {
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.browse_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.carName);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);
            holder.domainTextView = (TextView) convertView.findViewById(R.id.domain);
            holder.reviewTextView = (TextView) convertView.findViewById(R.id.review);

            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        AssessHistory source = mAssessHistories.get(position);
        convertView.setTag(R.id.tag_second, source);

        if ("false".equals(source.review)) {
            holder.reviewTextView.setVisibility(View.GONE);
        } else {
            holder.reviewTextView.setText("已下架");
            holder.reviewTextView.setVisibility(View.VISIBLE);
        }

        holder.domainTextView.setText(source.city + "车源");
        holder.tvCarTitle.setText(source.model_detail);
        holder.mileTextView.setText(source.year + "年上牌/" + source.mile + "万公里");
        holder.timeTextView.setText(source.create_time);
        if ("promo_car".equals(source.record_type)) {
            holder.priceTextView.setText(source.promo_price);
        } else {
            if ("".equals(source.price) || Utils.isLing(source.price)) {
                holder.priceTextView.setVisibility(View.GONE);
            } else {
                holder.priceTextView.setText(source.price + "万");
                holder.priceTextView.setVisibility(View.VISIBLE);
            }
        }
        Glide.with(mContext).load(source.url).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        return convertView;
    }

    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarTitle;

        public TextView timeTextView;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView domainTextView;

        public TextView reviewTextView;

    }
}
